package com.wei.czz.framework.common.controller;

import java.util.List;

import com.wei.czz.common.annotation.Auth;
import com.wei.czz.common.annotation.Resubmit;
import com.wei.czz.common.dto.common.NameValue;
import com.wei.czz.common.dto.common.messageFlow.MessageFlowDto;
import com.wei.czz.common.dto.common.messageFlow.MessageFlowEnumDto;
import com.wei.czz.common.dto.common.messageFlow.MessageFlowRetryDto;
import com.wei.czz.common.enums.rabbitMQ.MqEnum;
import com.wei.czz.common.jsr303.group.Search;
import com.wei.czz.common.property.BatchProperty;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.vo.common.messageFlow.MessageFlowFormVo;
import com.wei.czz.framework.common.manager.MessageFlowManager;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.wei.czz.common.tool.Result;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-05-12 17:12:18
 * className: MessageFlowController
 * version: 1.0
 * description:
 */
@RestController
@RequestMapping("/message-flow")
@AllArgsConstructor
public class MessageFlowController {

    private static final Logger log = LoggerFactory.getLogger(MessageFlowController.class);

    private final BatchProperty batchProperty;

    private final MessageFlowManager messageFlowManager;

    /**
     * 分页获取消息发送记录接口
     * @param messageFlowFormVo 参数对象
     * @return 结果
     */
    @PostMapping("/page/list")
    @Auth(permit = "message:flow:list")
    public Result<PageDto<MessageFlowDto>> getMessageFlowPageList(
            @Validated(Search.class) @RequestBody MessageFlowFormVo messageFlowFormVo) {
        log.info("开始分页获取消息发送记录业务。{}", messageFlowFormVo);

        PageDto<MessageFlowDto> pageDto = messageFlowManager.getMessageFlowPageList(messageFlowFormVo);

        log.info("分页获取消息发送记录业务结束");
        return Result.success(pageDto);
    }

    /**
     * 获取消息发送记录管理页面枚举接口
     * @return 结果
     */
    @GetMapping("/list/enum")
    @Auth(permit = "message:flow:list")
    public Result<MessageFlowEnumDto> getMessageFlowListEnum() {
        log.info("开始获取消息发送记录列表页面枚举");

        MessageFlowEnumDto messageFlowEnumDto = messageFlowManager.getMessageFlowListEnum();

        log.info("获取消息发送记录列表页面枚举结束");
        return Result.success(messageFlowEnumDto);
    }

    /**
     * 获取交换机枚举列表接口
     * @param queue 消息队列
     * @return 结果
     */
    @GetMapping("/exchange/enum-list")
    @Auth(permit = "message:flow:list")
    public Result<List<NameValue<String>>> getExchangeEnumList(@RequestParam(value = "queue", required = false) String queue) {
        log.info("开始获取交换机枚举列表业务。queue={}", queue);

        List<NameValue<String>> list;
        if (StringUtils.isBlank(queue)) {
            list = MqEnum.getExchangeList();
        } else {
            list = MqEnum.getExchangeList(queue);
        }

        log.info("获取交换机枚举列表业务结束");
        return Result.success(list);
    }

    /**
     * 获取消息队列枚举列表接口
     * @param exchange 交换机
     * @return 结果
     */
    @GetMapping("/queue/enum-list")
    @Auth(permit = "message:flow:list")
    public Result<List<NameValue<String>>> getQueueEnumList(@RequestParam("exchange") String exchange) {
        log.info("开始获取消息队列枚举列表业务。exchange={}", exchange);

        List<NameValue<String>> list;
        if (StringUtils.isBlank(exchange)) {
            list = MqEnum.getQueueList();
        } else {
            list = MqEnum.getQueueList(exchange);
        }

        log.info("获取消息队列枚举列表业务结束");
        return Result.success(list);
    }

    /**
     * 重试消息发送接口
     * @param idList 主键列表
     * @return 结果
     */
    @Resubmit(message = "重试消息发送")
    @PostMapping("/retry")
    @Auth(permit = "message:flow:retry")
    public Result<MessageFlowRetryDto> retrySendMessage(@RequestBody List<Long> idList) {
        log.info("开始重试消息发送业务。idList={}", idList);
        if (idList.isEmpty()) {
            return Result.clientFail("主键列表不能为空");
        }
        if (batchProperty.getMaxSize() < idList.size()) {
            log.warn("重试消息发送数量超限。batch.size={} size={}", batchProperty.getMaxSize(), idList.size());
            return Result.clientFail("一次重试消息发送最多允许同时发送" + idList.size() + "条消息");
        }

        MessageFlowRetryDto messageFlowRetryDto = messageFlowManager.retrySendMessage(idList);

        log.info("重试消息发送业务结束");
        return Result.success(messageFlowRetryDto);
    }

}
